package de.appQuest.picturecombiner;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class Bildkombinierer extends Activity {

	 final static int CAMERA_RESULT = 0;

	 private static final int REQ_CODE_PHOTO_TAKE = 1;

	//keep track of cropping intent
	 final int PIC_CROP = 2;
	 
	 private Uri picUri;

	  Button takePictureButton;
	  
	  TouchImageView  touchimageview1;
	  TouchImageView  touchimageview2;
	  TouchImageView  touchimageview3;
	  TouchImageView  touchimageview4;
	  TouchImageView  touchimageview5;
	  TouchImageView  touchimageview6;
	  Bitmap thePic1;
	  Bitmap thePic2;
	  Bitmap thePic3;
	  Bitmap thePic4;
	  Bitmap thePic5;
	  Bitmap thePic6;
	  
	  MyViewGroup viewgroup;
	  LinearLayout masterview;
	  	
	  	private int counter = 0;
	  	String name = "touchimageview";
	    private static final int SCAN_QR_CODE_REQUEST_CODE = 0;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bildkombinierer);
        
        takePictureButton = (Button) findViewById(R.id.TakePictureButton);
        
//        masterview = (LinearLayout)findViewById(R.id.masterview);
        
        touchimageview1 = new TouchImageView(this);
        touchimageview2 = new TouchImageView(this);
        touchimageview3 = new TouchImageView(this);
        touchimageview4 = new TouchImageView(this);
        touchimageview5 = new TouchImageView(this);
        touchimageview6 = new TouchImageView(this);
        viewgroup = (MyViewGroup) findViewById(R.id.screen);

        takePictureButton.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
            
        	  Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
              startActivityForResult(i, REQ_CODE_PHOTO_TAKE);
            
          }
          
        });


      }

      protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        
        if(resultCode != RESULT_OK) return;
        
        switch (requestCode) {
        case REQ_CODE_PHOTO_TAKE:
        	
        	picUri = intent.getData();
            
    		performCrop();
           
            break;
        case PIC_CROP:
        	
        	//get the returned data
        	Bundle extras = intent.getExtras();
        	//get the cropped bitmap
//        	thePic = extras.getParcelable("data");
        	counter = counter + 1;
        	//String count1=String.valueOf(conter);
        	
        	LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
    		        LayoutParams.WRAP_CONTENT);
        	
        	switch(counter)
        	{
        	case 1:
        		thePic1 = extras.getParcelable("data");
        		touchimageview1.setBitmap(thePic1);
        		viewgroup.addView(touchimageview1, lp);
        		
        		break;
        	case 2:
        		thePic2 = extras.getParcelable("data");
        		touchimageview2.setBitmap(thePic2);
        		viewgroup.addView(touchimageview2, lp);
        		break;
        	case 3:
        		thePic3 = extras.getParcelable("data");
        		touchimageview3.setBitmap(thePic3);
        		viewgroup.addView(touchimageview3, lp);
        		break;
        	case 4:
        		touchimageview4.setBitmap(thePic4);
        		masterview.addView(touchimageview4, lp);
        		break;
        	case 5:
        		touchimageview5.setBitmap(thePic5);
        		masterview.addView(touchimageview5, lp);
        		break;
        	case 6:
        		touchimageview6.setBitmap(thePic6);
        		masterview.addView(touchimageview6, lp);
        		break;
        	}
        	viewgroup.computeScroll();
        	break;
        default:
        	if (requestCode == SCAN_QR_CODE_REQUEST_CODE) 
    		{
    			if (resultCode == RESULT_OK) 
    			{
    				String logMsg = intent.getStringExtra("SCAN_RESULT");
    				
    				log(logMsg);
    			}
    		}
            break;
        }
      }
        
        private void performCrop(){
        	
        	try {
        	    //call the standard crop action intent (the user device may not support it)
        		Intent cropIntent = new Intent("com.android.camera.action.CROP");
        		    //indicate image type and Uri
        		cropIntent.setDataAndType(picUri, "image/*");
        		    //set crop properties
        		cropIntent.putExtra("crop", "true");
        		    //indicate aspect of desired crop
        		cropIntent.putExtra("aspectX", 1);
        		cropIntent.putExtra("aspectY", 1);
        		    //indicate output X and Y
        		cropIntent.putExtra("outputX", 256);
        		cropIntent.putExtra("outputY", 256);
        		    //retrieve data on return
        		cropIntent.putExtra("return-data", true);
        		    //start the activity - we handle returning in onActivityResult
        		startActivityForResult(cropIntent, PIC_CROP);
        	}
        	catch(ActivityNotFoundException anfe){
        	    //display an error message
        	    String errorMessage = "Whoops - your device doesn't support the crop action!";
        	    Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
        	    toast.show();
        	}
        }
        
    
        
        
        
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    	MenuItem menuItem = menu.add("Log");
    	menuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {

    		public boolean onMenuItemClick(MenuItem item) 
    		{
    			Intent intent = new Intent("com.google.zxing.client.android.SCAN");
    			intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
    			startActivityForResult(intent, SCAN_QR_CODE_REQUEST_CODE);
    			return false;
    		}
    	});

    		return super.onCreateOptionsMenu(menu);
    	}

    	
    	private void log(String qrCode) 
    	{
    		Intent intent = new Intent("ch.appquest.intent.LOG");

    		if (getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY).isEmpty()) 
    		{
    				Toast.makeText(this, "Logbook App not Installed", Toast.LENGTH_LONG).show();
    				return;
    		}

    		intent.putExtra("ch.appquest.taskname", "Metalldetektor");
    		intent.putExtra("ch.appquest.logmessage", qrCode);

    		startActivity(intent);
    	}

    	@Override
    	public void onAttachedToWindow() {
    	    super.onAttachedToWindow();
    	    openOptionsMenu();
    	}
    }



















